#region License Revision: 0 Last Revised: 6/18/2008 11:00 AM
/******************************************************************************
Copyright (c) Microsoft Corporation.  All rights reserved.


This file is licensed under the Microsoft Public License (Ms-PL). A copy of the Ms-PL should accompany this file. 
If it does not, you can obtain a copy from: 

http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
******************************************************************************/
#endregion // License
// VIEWS Sample
// Common Library for Silverlight Popup
// MS Research 2008
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Browser;
using ScriptInterop;

// Silverlight Popup namespace
namespace SilverlightPopup
{
	/// <summary>
	/// Popup class
	/// </summary>
	[ScriptableType]
	public class Popup
	{
		/// <summary>
		/// Popup page
		/// </summary>
		private Page pg;
		/// <summary>
		/// Initialize the Popup
		/// </summary>
		/// <param name="page">silverlight Page that is the popup</param>
		public Popup(Page page)
		{
			pg = page;
			pg.Explode.Completed += new EventHandler(Explode_Completed);
			HidePopup();
		}

		/// <summary>
		/// Expolde Animation is completed
		/// </summary>
		/// <param name="sender">sending object</param>
		/// <param name="e">event arguments</param>
		void Explode_Completed(object sender, EventArgs e)
		{
			// resize to a large size, so our auto resize code can size it back down
			//Page.ResizePopup(800, 800);
		}

		/// <summary>
		/// Shows the Pop, with the appropriate content
		/// </summary>
		/// <param name="popData">the data structure holding the popup information</param>
		[ScriptableMember]
		public void ShowPopup(object popData)
		{
			PopupData popupData = new PopupData((ScriptObject)popData);
			try
			{
				object xamlTree = XamlReader.Load((string)popupData.Xaml);
				pg.stackPanel.Children.Clear();
				pg.Opacity = 0;// make it invisible so we can resize

				// increase size of the DIV, so autolayout and sizing can fit it to the correct size
				HtmlElement elem = HtmlPage.Document.GetElementById("popuphost");

				// move to top left so we have as much room as possible
				elem.SetStyleAttribute("position", "relative");
				elem.SetStyleAttribute("left", "0");
				elem.SetStyleAttribute("top", "0");
				pg.ComputeSize = true; // we want to compute the size of the new content
				Page.ResizePopup(600, 600); // resize to maximum possible size, autoresize will size it down

				// move to proper location
				elem.SetStyleAttribute("position", "absolute");
				elem.SetStyleAttribute("left", popupData.X.ToString());
				elem.SetStyleAttribute("top", popupData.Y.ToString());
				pg.stackPanel.Children.Add((UIElement)xamlTree);

				// run the explode storyboard
				pg.Opacity = 1;
				elem.SetStyleAttribute("visibility", "visible");
				pg.Explode.Begin();
			}
			catch (Exception e)
			{
				// on error just show the error information
				pg.stackPanel.Children.Clear();
				TextBlock tb = new TextBlock();
				tb.Text = e.ToString();
				pg.ComputeSize = true; // we want to compute the size of the new content
				pg.stackPanel.Children.Add(tb);
				pg.Opacity = 1;// make it visible
			}
		}

		/// <summary>
		/// Hide the popup
		/// </summary>
		/// <param name="popupData">popupData structure if called externally, null if called internally</param>
		[ScriptableMember]
		public void HidePopup()
		{
			try
			{
				HtmlElement elem = HtmlPage.Document.GetElementById("popuphost");
				pg.Opacity = 0;// make it invisible so we can resize
				elem.SetStyleAttribute("visibility", "hidden");
			}
			catch (Exception e)
			{
				pg.stackPanel.Children.Clear();
				TextBlock tb = new TextBlock();
				tb.Text = e.ToString();
				pg.ComputeSize = true; // we want to compute the size of the new content
				pg.stackPanel.Children.Add(tb);
				pg.Opacity = 1;// make it visible
			}

		}

	}
}
